package it.afs.worklog.application.form.admin;

import it.afs.worklog.Cliente;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;

/**
 * @author Fanta
 */
public class ClienteFormPage extends AdminPage
{

	public ClienteFormPage()
	{
		super();
		addComponents(new Cliente());
	}

	public ClienteFormPage(PageParameters parameters)
	{
		super(parameters);
		addComponents(getFactory().getDao().getCliente(parameters.getString("id")));
	}
	
	private void addComponents(Cliente object)
	{
		addComponents();
		
		add(new Link<Void>("back")
		{
			private static final long serialVersionUID = -8772284177591533024L;

			@Override
			public void onClick()
			{
				setResponsePage(ClientiPage.class);
			}
		});
		
		if(object.isNew())
		{
			object.setAttivo(true);
		}
		
		Form<Cliente> form = new Form<Cliente>("form", new CompoundPropertyModel<Cliente>(object))
		{
			private static final long serialVersionUID = 6857195340353558421L;

			@Override
			protected void onSubmit()
			{
				super.onSubmit();
				if(getFactory().getDao().set(getModelObject()))
				{
					setResponsePage(ClientiPage.class);
				}
				else
				{
					error(getString("submit.error"));
				}
			}
		};
		add(form);
		
		form.add(new RequiredTextField<String>("nominativo"));
		form.add(new TextField<String>("riferimenti"));
		form.add(new CheckBox("attivo"));
		
		form.add(new FeedbackPanel("feedback"));
	}
}